import router from '../router'
import { setDocumentTitle, domTitle } from '@/utils/domUtil'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css'
import Vue from "vue";
import { ACCESS_TOKEN } from '@/store/mutation-types'
import '@crms/style.less';

const forbiddenHttpRequest = false;


NProgress.configure({ showSpinner: false }); // NProgress Configuration
//  路由白名单
const whiteList = [];

router.beforeEach((to, from, next) => {
    NProgress.start();
    to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`));
    
    //  验证是否登录
    if (Vue.ls.get(ACCESS_TOKEN)) {
        /* has token */
        if (to.path === '/user/login') {
            next({ name: 'index' });
            NProgress.done()
        } else {
            next()
        }
    } else {
        if ( to.path === '/user/login' ) {
            next();
        } else if (whiteList.includes(to.name)) {
            // 在免登录白名单，直接进入
            next()
        } else {
            next({ path: '/user/login', query: { redirect: to.fullPath } });
            NProgress.done();
        }
    }
});


router.afterEach(() => {
    NProgress.done() // finish progress bar
});


